﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SubSonic;

public partial class webUserControl_CustomerTimeUp : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Cookies aCookies = new Cookies();
            int flag = int.Parse(aCookies.getCookies("cFlag"));
            int isType = int.Parse(aCookies.getCookies("cisType"));
            int districtID = int.Parse(aCookies.getCookies("cDistrictID"));
            int cityID = int.Parse(aCookies.getCookies("cCityID"));
            
            if (flag == 1)
            {
                AddItems(ddlCityID, "sysCityName", "0", 0);
                //ddlCityID.Enabled = false;

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();
                ListItem _li = new ListItem("全部显示", "0");
                ddlDistrictID.Items.Add(_li);
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    ddlDistrictID.Items.Add(li);
                }
                GridView1.DataSource = reDataset();
                GridView1.DataBind();
            }

            if (flag == 0)
            {
                AddItems(ddlCityID, "sysCityName", cityID.ToString(), 0);
                ddlCityID.Enabled = false;

                ddlDistrictID.Items.Clear();
                Query q = new Query("sysDistrictName");
                q.AddWhere("cityid", ddlCityID.SelectedValue);
                IDataReader dr = q.ExecuteReader();
                ListItem _li= new ListItem("全部显示","0");
                ddlDistrictID.Items.Add(_li);
                while (dr.Read())
                {
                    ListItem li = new ListItem();
                    li.Value = dr[0].ToString();
                    li.Text = dr[1].ToString();
                    ddlDistrictID.Items.Add(li);
                }
                GridView1.DataSource = reDataset();
                GridView1.DataBind();
            }
        }
    }
    protected void ddlCityID_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlDistrictID.Items.Clear();
        if(ddlCityID.SelectedValue=="0")
        {
            ListItem _li = new ListItem("全部显示", "0");
            ddlDistrictID.Items.Add(_li);
        }
        Query q = new Query("sysDistrictName");
        q.AddWhere("cityid", ddlCityID.SelectedValue);
        IDataReader dr = q.ExecuteReader();
        while (dr.Read())
        {
            ListItem li = new ListItem();
            li.Value = dr[0].ToString();
            li.Text = dr[1].ToString();
            ddlDistrictID.Items.Add(li);
        }
    }
    static void AddItems(DropDownList ddl, string tb, string id, int isType)
    {
        if (isType == 1)
        {
            for (int i = 0; i <= 10; i++)
            {
                ListItem li = new ListItem();
                li.Value = i.ToString();
                li.Text = i.ToString();
                if (id == i.ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }
        else
        {
            Query q = new Query(tb);
            IDataReader dr = q.ExecuteReader();
            ListItem _li = new ListItem("全部显示", "0");
            ddl.Items.Add(_li);
            while (dr.Read())
            {
                ListItem li = new ListItem();
                li.Value = dr[0].ToString();
                li.Text = dr[1].ToString();
                if (id == dr[0].ToString())
                {
                    li.Selected = true;
                }
                ddl.Items.Add(li);
            }
        }

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        GridView1.DataSource = reDataset();
        GridView1.DataBind();
       
    }
    public  DataSet reDataset()
    {
        SqlQuery q = new SqlQuery();
        q.From("vw_CustomerInfo");
        if (tbCustomerName.Text != "")
        {
            q.AndExpression("CustomerName").Like("%" + tbCustomerName.Text + "%");
        }
        if (ddlCityID.SelectedValue != "0")
        {
            q.AndExpression("cityID").IsEqualTo(ddlCityID.SelectedValue);
        }
        //if (ddlDistrictID.SelectedValue != "0")
        //{
        //    q.AndExpression("districtid").IsEqualTo(ddlDistrictID.SelectedValue);
        //}
        if (tbOutDate.Text != "")
        {
            q.AndExpression("outTime").IsLessThanOrEqualTo(tbOutDate.Text);
        }
        return q.ExecuteDataSet();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName =="Del")
        {
            Literal1.Text = "";
             Query _q = new Query("CustomerInfo");
            _q.AddWhere("ID", e.CommandArgument);
            IDataReader dr = _q.ExecuteReader();
            if(dr.Read())
            {
                if(dr[6].ToString()==dr[7].ToString())
                {
                    StoredProcedure sp = new StoredProcedure("sp_CustomerTimeOut");
                    sp.Command.AddParameter("@cID", e.CommandArgument);
                    sp.Execute();
                    Query q = new Query("CustomerInfo");
                    q.AddWhere("ID", e.CommandArgument);
                    q.AddUpdateSetting("isStatus", 0);
                    q.Execute();
                    Literal1.Text = "<script language=javascript>alert('提前释放成功！');</script>";
                }
                else
                {
                    Response.Redirect("~/myPoints.aspx?cID="+e.CommandArgument);
                }
            }
            
            //myDateBind();
            
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        SqlQuery q = new SqlQuery();
        q.From("vw_CustomerInfo");
        if (tbCustomerName.Text != "")
        {
            q.AndExpression("CustomerInfo").Like(tbCustomerName.Text);
        }
        if (ddlCityID.SelectedValue != "0")
        {
            q.AndExpression("cityID").IsEqualTo(ddlCityID.SelectedValue);
        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AndExpression("districtid").IsEqualTo(ddlDistrictID.SelectedValue);
        }
        if (tbOutDate.Text != "")
        {
            q.AndExpression("outTime").IsLessThanOrEqualTo(tbOutDate.Text);
        }
        ViewState["sortColumn"] = e.SortExpression.ToString();
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
            q.OrderAsc(ViewState["sortColumn"].ToString());
        }
        else
        {
            if ("ASC" == ViewState["sortDirection"].ToString())
            {
                ViewState["sortDirection"] = "DESC";
                q.OrderDesc(ViewState["sortColumn"].ToString());
            }
            else
            {
                ViewState["sortDirection"] = "ASC";
                q.OrderAsc(ViewState["sortColumn"].ToString());
            }
        }
        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SqlQuery q = new SqlQuery();
        q.From("vw_CustomerInfo");
        if (tbCustomerName.Text != "")
        {
            q.AndExpression("CustomerInfo").Like(tbCustomerName.Text);
        }
        if (ddlCityID.SelectedValue != "0")
        {
            q.AndExpression("cityID").IsEqualTo(ddlCityID.SelectedValue);
        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AndExpression("districtid").IsEqualTo(ddlDistrictID.SelectedValue);
        }
        if (tbOutDate.Text != "")
        {
            q.AndExpression("outTime").IsLessThanOrEqualTo(tbOutDate.Text);
        }
        if (ViewState["sortColumn"]==null)
        {
            ViewState["sortColumn"] = "ID";
        }
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        if (ViewState["sortDirection"].ToString()=="ASC")
        {
            q.OrderAsc(ViewState["sortColumn"].ToString());
        }
        else
        {
            q.OrderDesc(ViewState["sortColumn"].ToString());
        }
        
        GridView1.DataSource = q.ExecuteDataSet();
        int startIndex;
        startIndex = GridView1.PageIndex * GridView1.PageSize;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    public void myDateBind()
    {
        SqlQuery q = new SqlQuery();
        q.From("vw_CustomerInfo");
        if (tbCustomerName.Text != "")
        {
            q.AndExpression("CustomerInfo").Like(tbCustomerName.Text);
        }
        if (ddlCityID.SelectedValue != "0")
        {
            q.AndExpression("cityID").IsEqualTo(ddlCityID.SelectedValue);
        }
        if (ddlDistrictID.SelectedValue != "0")
        {
            q.AndExpression("districtid").IsEqualTo(ddlDistrictID.SelectedValue);
        }
        if (tbOutDate.Text != "")
        {
            q.AndExpression("outTime").IsLessThanOrEqualTo(tbOutDate.Text);
        }
        if (ViewState["sortColumn"] == null)
        {
            ViewState["sortColumn"] = "ID";
        }
        if (ViewState["sortDirection"] == null)
        {
            ViewState["sortDirection"] = "ASC";
        }
        if (ViewState["sortDirection"].ToString() == "ASC")
        {
            q.OrderAsc(ViewState["sortColumn"].ToString());
        }
        else
        {
            q.OrderDesc(ViewState["sortColumn"].ToString());
        }

        GridView1.DataSource = q.ExecuteDataSet();
        GridView1.PageIndex = GridView1.SelectedIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Cookies aCookies=new Cookies();
        if(aCookies.getCookies("cisType")=="3")
        {
            GridView1.Columns[14].Visible = true;
        }
        else
        {
            GridView1.Columns[14].Visible = false;

        }
    }
}
